package com.qyhl.webtv.module_user.login.modifypwd;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.orhanobut.hawk.Hawk;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.Md5Builder;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.dialog.LoadingDialog;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.utils.eventbus.BusFactory;
import com.qyhl.webtv.commonlib.utils.eventbus.Event;
import com.qyhl.webtv.module_user.R;
import com.qyhl.webtv.module_user.R2;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;

/**
 * 找回密码
 */
@Route(path = ARouterPathConstant.NEWMODIFY)
public class NewModifyActivity extends BaseActivity implements NewModifyContract.NewModifyView {
    @BindView(R2.id.phononum)
    EditText phononum;
    @BindView(R2.id.editcode)
    EditText editcode;
    @BindView(R2.id.editpassword)
    EditText editpassword;
    @BindView(R2.id.editconfirmpassword)
    EditText editconfirmpassword;
    @BindView(R2.id.confirm)
    Button confirm;
    @BindView(R2.id.btncode)
    Button btncode;

    private LoadingDialog.Builder builder;
    private CountDownTimer mTimer;
    private NewModifyContract.NewModifyPresenterModel presenterModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }



    private void initView() {
        presenterModel = new NewModifyPresenterModel(this);

        builder = new LoadingDialog.Builder(this);
        builder.setTitle("正在提交...");
        builder.setCanceledOnTouchOutside(false);
        builder.setCancelable(true);

        //配置计时器
        mTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                btncode.setText(millisUntilFinished / 1000 + "s后重新获取");
            }

            @Override
            public void onFinish() {
                resetCodeBtn();
            }
        };

        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(editconfirmpassword.getText().toString()) ||
                        TextUtils.isEmpty(phononum.getText().toString()) ||
                        TextUtils.isEmpty(editpassword.getText().toString()) ||
                        TextUtils.isEmpty(editcode.getText().toString())) {
                    confirm.setEnabled(false);
                } else {
                    confirm.setEnabled(true);
                }
            }
        };
        editconfirmpassword.addTextChangedListener(textWatcher);
        phononum.addTextChangedListener(textWatcher);
        editcode.addTextChangedListener(textWatcher);
        editpassword.addTextChangedListener(textWatcher);
    }

    @Override
    protected void setListener() {

    }

    private void resetCodeBtn() {
        if (mTimer != null) {
            mTimer.cancel();
        }
        btncode.setText("获取验证码");
        btncode.setEnabled(true);
    }

    @OnClick({R2.id.login_back, R2.id.confirm, R2.id.btncode})
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.login_back) {
            finish();

        } else if (i == R.id.btncode) {
            getCode();

        } else if (i == R.id.confirm) {
            goConfirm();

        } else {
        }
    }

    private void goConfirm() {
        if (!StringUtils.isMobileNO(phononum.getText().toString())) {
            showToast("请输入正确的手机号", ERROR);
            return;
        }
        String pwd = editpassword.getText().toString();
        if (pwd.length() < 6) {
            Toasty.normal(this, "密码应为6-20位", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!pwd.equals(editconfirmpassword.getText().toString())) {
            Toasty.normal(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        builder.show();
        //加密
        String password = Md5Builder.encode(pwd);
        presenterModel.goConfirm(phononum.getText().toString().trim(), password, editcode.getText().toString().trim());

    }

    private void getCode() {
        String code = phononum.getText().toString();
        if (StringUtils.isMobileNO(code)) {
            mTimer.start();
            btncode.setEnabled(false);
            presenterModel.getCode(code);
        } else {
            Toasty.normal(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void getCodeError(String s) {
        Toasty.normal(this, s, Toast.LENGTH_SHORT).show();
        resetCodeBtn();
    }

    @Override
    public void getCodeSuccess() {
        Toasty.normal(this, "验证码已发送，请耐心等待...", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void modifyError(String msg) {
        builder.dismiss();
        Toasty.normal(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void modifySuccess() {
        builder.dismiss();
        Toasty.normal(this, "密码修改成功！", Toast.LENGTH_SHORT).show();
        if (Hawk.contains("token")) {
            Hawk.delete("token");
        }
        if (Hawk.contains("login")) {
            Hawk.delete("login");
        }
        BusFactory.getBus().post(new Event.LoginMessage(false));
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //umeng
        MobclickAgent.onPageStart("找回密码");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        //umeng
        MobclickAgent.onPageEnd("找回密码");

        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        super.onDestroy();
    }

    @Override
    protected int getContentView() {
        return R.layout.login_activity_new_modify;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        initView();
    }
}
